package cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldmanpointrecord;

import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.oldman.OldmanDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.oldman.OldmanService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.fengdong.smartelderly.controller.admin.oldmanpointrecord.vo.*;
import cn.iocoder.yudao.module.fengdong.smartelderly.dal.dataobject.oldmanpointrecord.OldmanPointRecordDO;
import cn.iocoder.yudao.module.fengdong.smartelderly.convert.oldmanpointrecord.OldmanPointRecordConvert;
import cn.iocoder.yudao.module.fengdong.smartelderly.service.oldmanpointrecord.OldmanPointRecordService;

@Tag(name = "管理后台 - 老人积分记录")
@RestController
@RequestMapping("/fengdong.smartelderly/oldman-point-record")
@Validated
public class OldmanPointRecordController {

    @Resource
    private OldmanPointRecordService oldmanPointRecordService;
    @Resource
    private OldmanService oldmanService;

    @PostMapping("/create")
    @Operation(summary = "创建老人积分记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:create')")
    public CommonResult<Long> createOldmanPointRecord(@Valid @RequestBody OMPointRecordCreateReqVO createReqVO) {
        return success(oldmanPointRecordService.createOldmanPointRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新老人积分记录")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:update')")
    public CommonResult<Boolean> updateOldmanPointRecord(@Valid @RequestBody OMPointRecordUpdateReqVO updateReqVO) {
        oldmanPointRecordService.updateOldmanPointRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除老人积分记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:delete')")
    public CommonResult<Boolean> deleteOldmanPointRecord(@RequestParam("id") Long id) {
        oldmanPointRecordService.deleteOldmanPointRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得老人积分记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:query')")
    public CommonResult<OldmanPointRecordRespVO> getOldmanPointRecord(@RequestParam("id") Long id) {
        OldmanPointRecordDO oldmanPointRecord = oldmanPointRecordService.getOldmanPointRecord(id);
        return success(OldmanPointRecordConvert.INSTANCE.convert(oldmanPointRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得老人积分记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:query')")
    public CommonResult<List<OldmanPointRecordRespVO>> getOldmanPointRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<OldmanPointRecordDO> list = oldmanPointRecordService.getOldmanPointRecordList(ids);
        return success(OldmanPointRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得老人积分记录分页")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:query')")
    public CommonResult<PageResult<OldmanByPointRecordRespVO>> getOldmanPointRecordPage(@Valid OMPointRecordPageReqVO pageVO) {
        PageResult<OldmanPointRecordDO> pageResult = oldmanPointRecordService.getOldmanPointRecordPage(pageVO);
        if (CollectionUtils.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }

        // 拼接结果返回
        List<OldmanDO> oldmanDOS = oldmanService.getUserList(
                convertSet(pageResult.getList(), OldmanPointRecordDO::getOldmanId));
        return success(OldmanPointRecordConvert.INSTANCE.convertPageByOld(pageResult, oldmanDOS));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出老人积分记录 Excel")
    @PreAuthorize("@ss.hasPermission('fengdong.smartelderly:oldman-point-record:export')")
    @OperateLog(type = EXPORT)
    public void exportOldmanPointRecordExcel(@Valid OMPointRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<OldmanPointRecordDO> list = oldmanPointRecordService.getOldmanPointRecordList(exportReqVO);
        // 导出 Excel
        List<OldmanPointRecordExcelVO> datas = OldmanPointRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "老人积分记录.xls", "数据", OldmanPointRecordExcelVO.class, datas);
    }

}
